import * as API from '@/services';
import { DEFAULT_FORM_LAYOUT, KfBizQuickFormProps, KfFloatingContextProps, KfInput, KfInputNumber, KfQuickForm, KfSwitch, KfTextArea } from 'kf-web-library';
import { Form } from 'antd';
import React, { FunctionComponent } from 'react';

const FormItem = Form.Item;
type QueryEntity = API.Journal;
type CreateEntity = API.Journal_Create;
type ModifyEntity = API.Journal_Modify;

type DataFormProps = {} & KfBizQuickFormProps & KfFloatingContextProps;

const QuickForm: FunctionComponent<DataFormProps> = (props) => {
  const { formType, bizId, ...rest } = props;

  return (
    <KfQuickForm
      formType={formType}
      formProps={{
        ...DEFAULT_FORM_LAYOUT,
      }}
      defaultQueryRequest={() =>
        API.queryJournal(bizId!).then((resp) => {
          if (resp.flag) {
            resp.data = {
              ...resp.data,
              regularPrice: (resp.data?.regularPrice || 0) / 100,
            } as any;
          }
          return new Promise((resolve) => resolve(resp));
        })
      }
      submitRequest={{
        create: (formValues) => API.createJournal({ ...formValues } as CreateEntity),
        modify: (formValues) => API.patchJournal({ ...formValues,
          regularPrice: (formValues.regularPrice || 0) * 100,
          id: bizId } as ModifyEntity),
      }}
      {...rest}
    >
      <FormItem name={nameof<ModifyEntity>((x) => x.regularPrice)}
                label="标准单期价格"
                rules={[{ required: true }]}
                help={"往期的定价"}
      >
        <KfInputNumber prefix={'￥'} />
      </FormItem>
    </KfQuickForm>
  );
};

export default QuickForm;
